package es.efor.ics.gev.dao.jpa;

import java.util.List;

import es.efor.ics.gev.dao.LookupDao;
import es.efor.ics.gev.model.Accion;
import es.efor.ics.gev.model.Cargo;
import es.efor.ics.gev.model.Concepto;
import es.efor.ics.gev.model.Departamento;
import es.efor.ics.gev.model.PerfilOrganizativo;
import es.efor.ics.gev.model.PerfilPersonal;
import es.efor.ics.gev.model.Role;
import es.efor.ics.gev.model.TipoAtributo;
import es.efor.ics.gev.model.TipoEvento;
import es.efor.ics.gev.model.TipoTitulacion;
import es.efor.ics.gev.model.Titulacion;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Repository;

import javax.persistence.PersistenceContext;
import javax.persistence.EntityManager;

/**
 * JPA implementation of LookupDao.
 * 
 * @author <a href="mailto:bwnoll@gmail.com">Bryan Noll</a>
 */
@Repository
public class LookupDaoJpa implements LookupDao {
	private Log log = LogFactory.getLog(LookupDaoJpa.class);

	@PersistenceContext
	EntityManager entityManager;

	/**
	 * {@inheritDoc}
	 */
	@SuppressWarnings("unchecked")
	public List<Role> getRoles() {
		log.debug("Retrieving all role names...");

		return entityManager.createQuery("select r from Role r order by name")
				.getResultList();
	}

	/**
	 * {@inheritDoc}
	 */
	@SuppressWarnings("unchecked")
	public List<Accion> getAcciones() {
		log.debug("Retrieving all accion names...");

		return entityManager.createQuery(
				"select a from Accion a order by nombreAccion").getResultList();
	}

	/**
	 * {@inheritDoc}
	 */
	@SuppressWarnings("unchecked")
	public List<Departamento> getDepartamentos() {
		log.debug("Retrieving all departamento names...");

		return entityManager.createQuery(
				"select d from Departamento d order by nombreDepartamento")
				.getResultList();
	}

	/**
	 * {@inheritDoc}
	 */
	@SuppressWarnings("unchecked")
	public List<TipoAtributo> getTiposAtributo() {
		log.debug("Retrieving all tipo atributo names...");

		return entityManager.createQuery(
				"select t from TipoAtributo t order by nombreTipo")
				.getResultList();
	}

	/**
	 * {@inheritDoc}
	 */
	@SuppressWarnings("unchecked")
	public List<PerfilOrganizativo> getPerfilesOrganizativos() {
		return entityManager
				.createQuery(
						"select p from PerfilOrganizativo p order by upper(p.nombrePerfil)")
				.getResultList();
	}

	/**
	 * {@inheritDoc}
	 */
	@SuppressWarnings("unchecked")
	public List<PerfilPersonal> getPerfilesPersonales() {
		return entityManager
				.createQuery(
						"select p from PerfilPersonal p order by upper(p.nombrePerfil)")
				.getResultList();
	}

	/**
	 * {@inheritDoc}
	 */
	@SuppressWarnings("unchecked")
	public List<TipoEvento> getTipologias() {
		return entityManager.createQuery(
				"select t from TipoEvento t order by upper(t.nombre)")
				.getResultList();
	}

	/**
	 * {@inheritDoc}
	 */
	@SuppressWarnings("unchecked")
	public List<Cargo> getCargos() {
		log.debug("Retrieving all cargo names...");
		return entityManager.createQuery(
				"select c from Cargo c order by upper(c.nombreCargo)")
				.getResultList();
	}

	/**
	 * {@inheritDoc}
	 */
	@SuppressWarnings("unchecked")
	public List<Concepto> getConceptosGasto() {
		log.debug("Retrieving all concepto names...");
		return entityManager.createQuery(
		"select c from Concepto c order by upper(c.concepto)")
		.getResultList();
	}

	/**
	 * {@inheritDoc}
	 */
	@SuppressWarnings("unchecked")
	public List<TipoTitulacion> getTiposTitulacion() {
		log.debug("Retrieving all tipos titulacion names...");
		return entityManager.createQuery(
		"select t from TipoTitulacion t order by upper(t.nombreTipo)")
		.getResultList();
	}

	/**
	 * {@inheritDoc}
	 */
	@SuppressWarnings("unchecked")
	public List<Titulacion> getTitulaciones() {
		log.debug("Retrieving all titulacion names...");
		return entityManager.createQuery(
		"select t from Titulacion t order by upper(t.nombreTitulacion)")
		.getResultList();
	}

}
